using Lzfy_Refund_Service.Models;

namespace Lzfy_Refund_Service.Repositories
{
    /// <summary>
    /// 密码更新任务数据访问接口
    /// </summary>
    public interface IPasswordUpdateTaskRepository
    {
        /// <summary>
        /// 创建密码更新任务
        /// </summary>
        /// <param name="task">任务信息</param>
        /// <returns>创建的任务ID</returns>
        Task<int> CreateTaskAsync(PasswordUpdateTask task);

        /// <summary>
        /// 根据任务ID获取任务信息
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <returns>任务信息</returns>
        Task<PasswordUpdateTask?> GetTaskByIdAsync(string taskId);

        /// <summary>
        /// 根据患者ID获取待执行的任务
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <returns>待执行的任务列表</returns>
        Task<List<PasswordUpdateTask>> GetPendingTasksByPatientIdAsync(string patientId);

        /// <summary>
        /// 获取需要执行的任务列表
        /// </summary>
        /// <param name="currentTime">当前时间</param>
        /// <param name="limit">限制数量</param>
        /// <returns>需要执行的任务列表</returns>
        Task<List<PasswordUpdateTask>> GetTasksToExecuteAsync(DateTime currentTime, int limit = 100);

        /// <summary>
        /// 更新任务状态
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <param name="status">新状态</param>
        /// <param name="executedTime">执行时间</param>
        /// <param name="errorMessage">错误信息</param>
        /// <returns>是否更新成功</returns>
        Task<bool> UpdateTaskStatusAsync(string taskId, string status, DateTime? executedTime = null, string? errorMessage = null);

        /// <summary>
        /// 增加任务重试次数
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <param name="errorMessage">错误信息</param>
        /// <returns>是否更新成功</returns>
        Task<bool> IncrementRetryCountAsync(string taskId, string? errorMessage = null);

        /// <summary>
        /// 取消任务
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <returns>是否取消成功</returns>
        Task<bool> CancelTaskAsync(string taskId);

        /// <summary>
        /// 取消患者的所有待执行任务
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <returns>取消的任务数量</returns>
        Task<int> CancelPendingTasksByPatientIdAsync(string patientId);

        /// <summary>
        /// 清理过期的已完成任务
        /// </summary>
        /// <param name="beforeDate">清理此日期之前的任务</param>
        /// <returns>清理的任务数量</returns>
        Task<int> CleanupCompletedTasksAsync(DateTime beforeDate);

        /// <summary>
        /// 获取任务统计信息
        /// </summary>
        /// <returns>任务统计信息</returns>
        Task<Dictionary<string, int>> GetTaskStatisticsAsync();
    }
}